package com.sikulifang.bank.simulation.service.bank;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.sql.SqlBuilder;
import com.sikulifang.bank.simulation.dto.QueryReceiptDTO;
import com.sikulifang.bank.simulation.dto.ReceiptDTO;
import com.sikulifang.bank.simulation.dto.ResponseReceiptDTO;
import com.sikulifang.bank.simulation.enums.BankEnum;
import com.sikulifang.bank.simulation.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.sql.SQLException;
import java.util.List;

import static com.sikulifang.bank.simulation.constant.TableConstants.DETAIL_TABLE_NAME;

/**
 * ClassName ABCBankMessageService
 * Description 农行
 *
 * @author Yat
 * Date 2023年6月12日, 0012 10:39
 * version 1.0
 */
@Slf4j
@Service
public class ABCBankMessage extends AbstractBankMessage {

    @Override
    public String getActionName(String message) {
        return null;
    }

    @Override
    protected String getType(String actionName) {
        return null;
    }

    @Override
    public String inlet(String message) {
        return null;
    }

    @Override
    public String bankCode() {
        return BankEnum.ABC.name();
    }

    /**
     * 查询电子回单
     *
     * @param queryReceipt 查询电子回单的参数
     */
    private R<ResponseReceiptDTO> queryReceipt(QueryReceiptDTO queryReceipt) {

        // 昨天时间
        DateTime now = new DateTime();
        DateTime yesterday = DateUtil.offsetDay(now, -1);
        String endDate = DateUtil.format(yesterday, DatePattern.PURE_DATE_PATTERN);

        // 查询文件的日期
        String startDate = queryReceipt.getStartDate();

        // 7天前的日期
        DateTime dateTime = DateUtil.offsetDay(yesterday, -6);
        String sevenDaysAgo = DateUtil.format(dateTime, DatePattern.PURE_DATE_PATTERN);

        if (StrUtil.isNotBlank(startDate)) {
            startDate = DateUtil.format(DateUtil.parse(startDate), DatePattern.PURE_DATE_PATTERN);
            if (Convert.toInt(sevenDaysAgo) > Convert.toInt(startDate)) {
                return R.failed(new ResponseReceiptDTO(), "此接口支持查询上日至七天前的PDF文件包");
            }
        }

        SqlBuilder builder = SqlBuilder.create().select("*")
                .from(DETAIL_TABLE_NAME)
                .where("status = " + 1)
                .append(" and  bank_code= '" + bankCode() + "'");
        if (CharSequenceUtil.isNotBlank(startDate)) {
            builder.append(" and tran_date >=" + startDate);
        }

        if (CharSequenceUtil.isNotBlank(endDate)) {
            builder.append(" and tran_date <=" + endDate);
        }

        try {
            ResponseReceiptDTO responseReceiptDTO = new ResponseReceiptDTO();

            List<Entity> query = db.query(builder.build());
            if (query.isEmpty()) {
                return R.failed(responseReceiptDTO, "无回单信息");
            }

            String filePath = "/home/tmp/" + IdUtil.objectId() + "/";
            // 创建临时目录
            File mkdir = FileUtil.mkdir(filePath);
            for (Entity entity : query) {
                String pdf = detailService.createPdf(entity);
                FileUtil.copy(FileUtil.file(pdf), mkdir, false);
            }

            responseReceiptDTO.setDataStr(filePath);
            return R.ok(responseReceiptDTO);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public R<Dict> dowReceipt(List<ReceiptDTO> receiptList) {
        return null;
    }
}
